# -*- coding: utf-8 -*-


from openerp import models
from openerp.osv import osv, fields, orm
from openerp.tools import float_compare

# 订单行上增加最低售价
class sale_order_line_ext(orm.Model):
    _inherit = 'sale.order.line'
    _columns = {
        'product_id': fields.many2one('product.product', 'Product', required=True, domain=[('sale_ok', '=', True)], change_default=True, readonly=True, states={'draft': [('readonly', False)]}, ondelete='restrict'),
        'floor_price': fields.float(string=u'最低售价'),
        }

    #判断成交价是否低于最低售价
    def onchange_price_unit(self, cr, uid, ids, product_id, price_unit, floor_price, context=None):
        #成本价小于等于0不判断
        if floor_price > 0 and float_compare(price_unit, floor_price, 3) < 0:
            return {'warning': {'title': u'警告', 'message': u'不能低于最低售价%s' % floor_price}, 'value': {'price_unit': floor_price}}

    #订单产品更改时把产品成本价保存到最低售价字段中
    def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
                          uom=False, qty_uos=0, uos=False, name='', partner_id=False,
                          lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None):
        vals = super(sale_order_line_ext, self).product_id_change(cr, uid, ids, pricelist, product, qty,
                                                                  uom, qty_uos, uos, name, partner_id,
                                                                  lang, update_tax, date_order, packaging, fiscal_position, flag, context)

        if vals['value'].has_key('price_unit'):
            vals['value']['floor_price'] = vals['value']['price_unit']
        return vals
